<?php
/**
 * 牌局日志备份和缓存实日志
 * Created by PhpStorm.
 * Keter
 * 2016/8/1
 * @守护进程
 */
class PayLogHandler extends ScriptBase
{
    /**
     * 游戏服保存的牌局日志KEY
     */
    const LOG_NAME = 'payLog';

    /**
     * 出错备份
     */
    const QUEEN_BACKUP_NAME = 'payLogBackup';

    /**
     * 当前脚本暂停标志
     * 同类结果为TRUE时则暂停和停止，如1
     */
    const SUSPEND_TAG = 'PAY_LOG_HANDLER_SUSPEND'; //暂停

    /**
     * 停止脚本标志
     */
    const STOP_TAG = 'PAY_LOG_HANDLER_STOP'; //停止

    /**
     * 读取数据处理条数  add by tmac
     */
    const CACHE_LIMIT_NUMBER = 1000;

    /**
     * 牌局日志Redis
     */
    protected $roomStatusRedis;

    /**
     * 逻辑代码
     */
    public function logic()
    {
        set_time_limit(0);

        //$this->addTest();

        while (true) {
            try {
                // 连接redis
                $this->roomStatusRedis = $this->redis('room_status');

                //停止脚本循环
                if ($this->roomStatusRedis->get(self::STOP_TAG)) {
                    break;
                }

                //暂停脚本循环
                if ($this->roomStatusRedis->get(self::SUSPEND_TAG)) {
                    continue;
                }
                /*
                //执行脚本
                $element = $this->roomStatusRedis->lrange(self::LOG_NAME, -1, -1);
                if ($element) {
                    $result      = $this->logHandler($element);
                    $lastElement = $this->roomStatusRedis->rpop(self::LOG_NAME);
                     //如果没有处理成功，则放入备份队列
                    if ($lastElement && !$result) {
                        $this->roomStatusRedis->lpush(self::QUEEN_BACKUP_NAME, $lastElement);
                    }
                }
                */

                //---add by tmac 2016-12-15

                //执行脚本
                //获取列表长度，来取条数
                $lenth = $this->roomStatusRedis->lLen( self::LOG_NAME );
                if( empty($lenth) ){
                    $this->deinitRedis('room_status');
                    sleep(1);
                    continue;
                }

                $lenth = $lenth <= self::CACHE_LIMIT_NUMBER ? $lenth : self::CACHE_LIMIT_NUMBER;
                for($i=0; $i<= $lenth; $i++ ){
                    $element = $this->roomStatusRedis->lrange( self::LOG_NAME, -1, -1 );
                    $result      = $this->logHandler($element);
                    $lastElement = $this->roomStatusRedis->rpop(self::LOG_NAME);
                    // 如果没有处理成功，则放入备份队列
                    if ( $lastElement && !$result ) {
                        $this->roomStatusRedis->lpush(self::QUEEN_BACKUP_NAME, $lastElement);
                    }
                }
                //---end add
                // 断开redis
                $this->deinitRedis('room_status');
                usleep(10000);
            } catch (Exception $e) {
                //--add by tmac 异常错误回滚处理
                //$this->roomStatusRedis->discard();
                $this->deinitRedis('room_status');
                //--end add
                $this->e($e->getMessage());
                sleep(1);
            }
        }
    }

    /**
     * 执行脚本
     * @param $redis
     * @param $flag    //add by tmac 2016-12-17
     */
    public function logHandler($result, $flag = true )
    {
        if ($result) {
            $flag && $result = reset($result);
            $str = $result;
            if  ($str) {
                $data = json_decode($str, true);
                $this->roomStatusRedis->multi();
                $result = $this->save2db($data);
                if ($result) {
                    $this->roomStatusRedis->exec();
                    return true;
                } else {
                    $this->roomStatusRedis->discard();
                    return false;
                }
            }
        }
    }

    /**
     * 写入牌局数据库
     * @param $data
     */
    public function save2db($data)
    {
        $payLog = new PayLogQueen();
        $payLog->isRedisLog(false);
        if (empty($data['dieTime'])) {
            $this->e('empty_die_time', $data);
            return false;
        }
        $payLog->setTableName(); //不分库分表
        return $payLog->saveLog($data);
    }
}
?>